<?php

class Exts_Controller_Action extends Zend_Controller_Action {

	protected $_notFound = '/';
	protected $_user;
	protected $_page = 1;
	protected $_session;
	protected $_config;
	protected $_db;
	protected $_auth;
	protected $_flashMessenger = null;
	
	/**
	 * True if in debug mode, readed from application.ini
	 *
	 * @var boolean
	 */
	protected $_debug = false;


	public function init() {

		// Initalize Session
		$this->_initSession();
		// Load defaul config
		$configFile = 'config.default.ini';
		if (APPLICATION_ENV != 'production') {
			$configFile = 'config.default.' . APPLICATION_ENV . '.ini';
		}

		$config = new Zend_Config_Ini(
				APPLICATION_PATH . '/configs/' . $configFile
		);

		$this->_config = $config;

		Zend_Registry::set('config', $config);

		// Averiguamos si esta logueado
		$this->_auth = Zend_Auth::getInstance();
		if (isset($this->_session->user)) {
			$this->_user = $this->_session->user;
			$this->_user->auth = $this->_auth;
			$this->view->user = $this->_user;


			if (@$this->_config->menu->show == "off") {
				$this->view->menuHtml = '';
			} else {
				/**
				 * Si se cargo previamente el menu, se lo paso a la vista
				 */
				if (isset($this->_session->menu)) {
					$this->view->menuHtml = $this->_session->menu;
				}
			}
		}
		// Get default adapter
		$this->_db = Zend_Db_Table::getDefaultAdapter();

		//Paginado
		$this->_page = ( $this->_hasParam('page') ) ? $this->_getParam('page') : 1;

		// Debug Model
		$this->_debug = ( APPLICATION_ENV != 'production' );
		$this->_flashMessenger = $this->_helper->getHelper('Message');
		
	}

	/**
	 * Devuelve true si la aplicacion esta siendo debugeada.
	 *
	 * @return bool true si la aplicacion esta siendo debugueada
	 */
	protected function isInDebugMode() {
		return $this->_debug;
	}

	private function _initNotLoginUser() {
		$this->view->headScript()->appendFile('/js/js/login.box.js');
		$this->view->headLink()->appendStylesheet('/js/css/login.box.css');
	}

	private function _initSession() {
		$this->_session = new Zend_Session_Namespace('exts');
	}

	protected function getCustomTitle() {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		return " " . $request->getControllerName() . " / " . $request->getActionName();
	}

	protected function setTitleIfExists() {
		$title = $this->getCustomTitle();
		if (empty($title))
			return;
		$this->view->headTitle($title);
	}

}

